package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author hutianyi
 * @Date 2020/2/25 14:32
 * @Version v1.0
 * @Description
 */
@Service
@Slf4j
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    /**
     * 分页查询
     * @param queryPageBean 前端页面发过来的数据( 当前页码, 每页条数, 查询条件)
     * @return 结果( 总记录数, 当前页要显示的10条检查组数据  )
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //设置分页插件初始参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //通过Dao获取Page对象
        Page<Role> page = roleDao.selectByCondition(queryPageBean.getQueryString());
        //通过Page对象，构建PageResult
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());

        return pageResult;
    }

    //查找全部角色
    @Override
    public List<Role> findAll() {
       return roleDao.findAll();
    }

    //添加角色
    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);
        for (Integer permissionId : permissionIds) {
            Map<String,Integer> permissionMap = new HashMap<>();
            permissionMap.put("role_id",role.getId());
            permissionMap.put("permission_id",permissionId);
            roleDao.addPermissionAndRole(permissionMap);
        }
        for (Integer menuId : menuIds) {
            Map<String,Integer> menuMap = new HashMap<>();
            menuMap.put("role_id",role.getId());
            menuMap.put("menu_id",menuId);
            roleDao.addMenuAndRole(menuMap);
        }
    }

    //通过ID查找角色
    @Override
    public Role findById(Integer id) {
        Role role = roleDao.findById(id);
        return role;
    }

    //通过角色ID查找权限
    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    //通过角色ID查找菜单
    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleDao.findMenuIdsByRoleId(id);
    }

    //编辑角色
    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.deletePermissionByRoleId(role.getId());
        roleDao.deleteMenuByRoleId(role.getId());

        for (Integer permissionId : permissionIds) {
            Map<String,Integer> permissionMap = new HashMap<>();
            permissionMap.put("role_id",role.getId());
            permissionMap.put("permission_id",permissionId);
            roleDao.addPermissionAndRole(permissionMap);
        }
        for (Integer menuId : menuIds) {
            Map<String,Integer> menuMap = new HashMap<>();
            menuMap.put("role_id",role.getId());
            menuMap.put("menu_id",menuId);
            roleDao.addMenuAndRole(menuMap);
        }

    }

    //通过ID删除
    @Override
    public void deleteById(Integer id) {
        Long permissionCount = roleDao.countPermissionIdsByRoleId(id);
        Long menuCount = roleDao.countMenuIdsByRoleId(id);
        if (permissionCount > 0 && menuCount > 0){
            throw new RuntimeException("角色关联了权限和菜单, 该角色不能被删除");
        } else if(permissionCount > 0 && menuCount == 0) {
            throw new RuntimeException("角色关联了权限, 该角色不能被删除");
        } else if(permissionCount == 0 && menuCount> 0) {
            throw new RuntimeException("角色关联了菜单, 该角色不能被删除");
        }else {
            roleDao.deleteById(id);
        }
    }
}
